<?php
/**
 * Created by PhpStorm.
 * User: 王凯林
 * Date: 18/3/24
 * Time: 19:12
 */
namespace App\Http\Controllers\H5\Shop;
use App\Common\Exceptions\AixinException;
use App\Common\Exceptions\AixinExceptionCode;
use App\Http\Controllers\Controller;
use App\Model\UserCard;
use Illuminate\Http\Request;

class UserCardsController extends Controller
{
    /*
     * 用户的账户列表
     */
    public function index(Request $request){
        //判断参数是否正确
        if($request->has('uid')){
            //使用门面方法查询出该用户的所有账户信息
            $accounts = UserCard::where('uid',$request->uid)->orderby('create_time','desc')->get();
            if($accounts){
                //查询到
                return ApiResponse($accounts);
            }else{
                //没有查询到
                return ApiResponse('','miss','1');
            }
        }else{
            throw new AixinException(AixinExceptionCode::MISSING_PARAM);
        }
    }

    /*
     * 添加用户提现账号
     */
    public function store(Request $request){
        $uid = $request->uid;
        $types = $request->types;
        $account = $request->account;
        $account_name = $request->account_name;
        $account_bank = $request->account_bank;
        $create_time = time();
        //判段是提交的账户资料是否为空
        if(empty($account)||empty($account_name)||empty($uid)){
            throw new AixinException(AixinExceptionCode::MISSING_PARAM);
        }
        //通过type判段账户类型，0为银行卡，1为支付宝；
        if($types==1){
            $data = [
                'uid'=>$uid,
                'types'=>$types,
                'account'=>$account,
                'account_name'=>$account_name,
                'create_time'=>$create_time
            ];
        }else{
            $data = [
                'uid'=>$uid,
                'types'=>$types,
                'account'=>$account,
                'account_name'=>$account_name,
                'account_bank'=>$account_bank,
                'create_time'=>$create_time
            ];
        }

        //往数据库中保存
        $rs = UserCard::create($data);
        if($rs){
            //添加成功
            return ajaxSuccess();
        }else{
            //添加失败
            return ajaxError();
        }
    }

    /*
     * 删除提现账号
     */
    public function destroy(Request $request){
        //抛出参数丢失异常
        if(!$request->has('card_id')){
            throw new AixinException(AixinExceptionCode::MISSING_PARAM);
        }else{

            //操作数据表...通过查询删除
            $rs = UserCard::where('id',$request->card_id)->delete();
            if($rs){
                //删除成功
                return ajaxSuccess();
            }else{
                //删除失败
                return ajaxError();
            }
        }

    }


}